﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DTO;
using DAL;
using BUS;
using Utility;
using System.Data.SqlClient;
public partial class Doanh_Nghiệp_Doanhnghiep_update : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            

            LoadDropDownList();
            Loaddata();
        }
    }
    public Object soluonghinh(int mamhang)
    {

        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
        cn.Open();
        SqlCommand cmd = new SqlCommand("select count(mamh) from hinhanh where mamh='" + mamhang + "'", cn);
        return cmd.ExecuteScalar();


    }
    private void Loaddata()
    {
        int id = int.Parse(Request.QueryString["id"]);
        //string opendeal = (string)(Session["opendeal"].ToString());
        
        //if (opendeal == "opendeal")
        //{
        //    // chỉ cho cập nhật tình trạng
        //    FileUpload1.Visible = false;
          

        //}
        //else
        //{
        //    // cập nhật bình thường
        //    lit_TenSP.Enabled = true;
        //    lit_dongia.Enabled = true;
        //    lit_slphieuban.Enabled = true;
        //    lit_slphieuconlai.Enabled = true;
        //    lit_giamgia.Enabled = true;
        //    lit_chietkhau.Enabled = true;
        //    lit_doanhnghiep.Visible = false;
        //    ddl_doanhnghiep.Visible = true;
        //    lit_khuvuc.Visible = false;
        //    ddl_khuvuc.Visible = true;
        //    lit_Batdau.Enabled = true;
        //    lit_ketthuc.Enabled = true;


        //}
        MatHang_DTO mathang = MATHANG_BUS.GetByID(id);
        lit_MaSP.Text = mathang.MaMH.ToString();
        lit_TenSP.Text = mathang.TenMH;
        lit_dongia.Text = mathang.GiaBan.ToString();
        lit_slphieuban.Text = mathang.SoLuongPhieuBan.ToString();
        lit_slphieuconlai.Text = mathang.SoLuongPhieuConLai.ToString();
        lit_giamgia.Text = mathang.TiLeGiam.ToString();
        //lit_phigiaohang.Text = mathang.PhiGiaoHangTanNoi.ToString();
        lit_chietkhau.Text = mathang.PhanTram_ChietKhau.ToString();
        DoanhNghiep_DTO doanhnghiep = DoanhNghiep_BUS.GetByID(mathang.MaDN);
        lit_doanhnghiep.Text = doanhnghiep.TenDN;
        KHUVUC_DTO khuvuc = KHUVUC_BUS.GetByID(mathang.MaKhuVuc);
        lit_khuvuc.Text = khuvuc.TenKhuVuc;
        //loai = int.Parse(mathang.MaLoai.ToString());
       // doanh_nghiep = mathang.MaDN;
       // kvuc = mathang.MaKhuVuc;
        ddl_loaisanpham.SelectedValue = mathang.MaLoai.ToString();
        ddl_khuvuc.SelectedValue = mathang.MaKhuVuc.ToString();
        ddl_doanhnghiep.SelectedValue = mathang.MaDN.ToString();
        ddl_tinhtrang.SelectedValue = mathang.MaTinhTrang_SP.ToString();
        img_Hinhanh.ImageUrl = "~/photos/thumb/" + mathang.HinhMinhHoa;
        lit_Batdau.Text = mathang.ThoiGian_BatDau.ToString();
        lit_ketthuc.Text = mathang.ThoiGian_KetThuc.ToString();
        // thông tin ghi chú từ admin
        DieuKienSuDung_DTO dk = DieuKienSuDung_BUS.GetByID(mathang.MaMH);
        if(dk.GhiChu == "" )
        {
            txtghichu.Text = "chờ duyệt";
        }
        else
        {
            txtghichu.Text = dk.GhiChu.ToString();
        }
       
        
        LoaiHang_DTO loaihang = LOAIHANG_BUS.GetByID(mathang.MaLoai);

        lit_loaisanpham.Text = loaihang.TenLoai;

        lit_mota.Text = mathang.MoTa;
       
        tbHinhAnhCollection hinhanh = HinhAnh_BUS.GetByMaMH(id);
        Repeater1.DataSource = hinhanh;
        Repeater1.DataBind();

    }
    private void LoadDropDownList()
    {
        tbKhuVucCollection khuvuc = KHUVUC_BUS.GetAll();
        ddl_khuvuc.DataTextField = tbKhuVucColumns.TenKhuVuc.ToString();
        ddl_khuvuc.DataValueField = tbKhuVucColumns.MaKhuVuc.ToString();
        ddl_khuvuc.DataSource = khuvuc;
        ddl_khuvuc.DataBind();
        ddl_khuvuc.Items.Insert(0, new ListItem("All", "0"));

        LoaiHangColection loaihang = LOAIHANG_BUS.GetAll();
        ddl_loaisanpham.DataTextField = LOAIHANGCOLUMNS.TenLoai.ToString();
        ddl_loaisanpham.DataValueField = LOAIHANGCOLUMNS.MaLoai.ToString();
        ddl_loaisanpham.DataSource = loaihang;
        ddl_loaisanpham.DataBind();
        ddl_loaisanpham.Items.Insert(0, new ListItem("All", "0"));

        tbDoanhNghiepCollection doanhnghiep = DoanhNghiep_BUS.GetAll();
        ddl_doanhnghiep.DataTextField = tbDoanhNghiepColumns.TenDN.ToString();
        ddl_doanhnghiep.DataValueField = tbDoanhNghiepColumns.MaDN.ToString();
        ddl_doanhnghiep.DataSource = doanhnghiep;
        ddl_doanhnghiep.DataBind();
        ddl_doanhnghiep.Items.Insert(0, new ListItem("All", "0"));

        TinhTrang_SanPhamCollection tinhtrang_sp = TinhTrang_SanPham_BUS.GetAll();
        ddl_tinhtrang.DataTextField = TinhTrang_SanPhamColumns.TenTinhTrang.ToString();
        ddl_tinhtrang.DataValueField = TinhTrang_SanPhamColumns.MaTinhTrang_SP.ToString();
      
        ddl_tinhtrang.DataSource = tinhtrang_sp;
        ddl_tinhtrang.DataBind();
       
        //ddl_tinhtrang.Items.Insert(0, new ListItem("All", "0"));
        
        


    }
    protected void btn_capnhat_Click(object sender, EventArgs e)
    {
        int id = int.Parse(Request.QueryString["id"]);
        MatHang_DTO mathang = MATHANG_BUS.GetByID(id);
        mathang.MaMH = int.Parse(lit_MaSP.Text);
        mathang.TenMH = lit_TenSP.Text;
        mathang.GiaBan = float.Parse(lit_dongia.Text);
        mathang.SoLuongPhieuBan = int.Parse(lit_slphieuban.Text);
        mathang.SoLuongPhieuConLai = int.Parse(lit_slphieuconlai.Text);
        mathang.TiLeGiam = float.Parse(lit_giamgia.Text);
       // mathang.PhiGiaoHangTanNoi = int.Parse(lit_phigiaohang.Text);
        mathang.PhanTram_ChietKhau = float.Parse(lit_chietkhau.Text);
        mathang.MaLoai = int.Parse(ddl_loaisanpham.SelectedValue);
        mathang.MaDN = int.Parse(ddl_doanhnghiep.SelectedValue);
        mathang.MaKhuVuc = int.Parse(ddl_khuvuc.SelectedValue);
        mathang.MaTinhTrang_SP = int.Parse(ddl_tinhtrang.SelectedValue);
        mathang.ThoiGian_BatDau = DateTime.Parse(lit_Batdau.Text);
        mathang.ThoiGian_KetThuc = DateTime.Parse(lit_ketthuc.Text);

        mathang.MoTa = lit_mota.Text;
        mathang.HinhMinhHoa = "no_images.jpg";

        
        MATHANG_BUS.update(mathang);
        #region"Thêm Hình Chi Tiết"
        HttpFileCollection hfc = Request.Files;

        for (int i = 0; i < hfc.Count; i++)
        {
            HttpPostedFile hpf = hfc[i];

            if (hpf.ContentLength > 0)
            {

                hpf.SaveAs(Server.MapPath("~/photos/") + System.IO.Path.GetFileName(hpf.FileName));

                string link = System.IO.Path.GetFileName(hpf.FileName);


                System.Drawing.Image img = System.Drawing.Image.FromFile(
                    Server.MapPath("~/photos/" + link));

                ImageUtility.MakeThumbnailPhoto(
                    img, 100, 100, Server.MapPath("~/photos/thumb/"), link);

                HinhAnh_DTO hinhct = new HinhAnh_DTO();
                hinhct.Hinh = link;
                hinhct.MaMH = id;
                HinhAnh_BUS.Insert(hinhct);
            }



            //Response.Redirect("~/admin/A_Mathang.aspx");
        }
        #endregion


        //Response.Redirect(Request.RawUrl);

        //}
        //catch (Exception ex)
        //{
        //    //    Response.Redirect("~/Admin/DanhSachSanPham.aspx");
        //}
    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < Repeater1.Items.Count; i++)
            {
                CheckBox cxoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
                if (cxoa.Checked == true)
                {
                    HiddenField hdf = (HiddenField)Repeater1.Items[i].FindControl("hdmahinh");
                    int ma = int.Parse(hdf.Value);
                    HinhAnh_BUS.Delete(ma);

                }
            }
            Response.Redirect(Request.RawUrl);
        }
        catch (Exception ex)
        {
            ex.Message.ToString();
        }

    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            HinhAnh_DTO hinh = (HinhAnh_DTO)e.Item.DataItem;
            Image h = (Image)e.Item.FindControl("hinhminhhoa");
            h.ImageUrl = "~/photos/thumb/" + hinh.Hinh;
        }
    }
}

